/*
  ODBuzzer.cpp - 
*/

#include "ODBuzzer.h"
#include "ODPort.h"
#include "WProgram.h"
#include "ODADebug.h"

Buzzer::Buzzer() {
  return;
}

void Buzzer::init(uint8_t pin) {
  _port.initOutput(pin, LOW);
  _stopTime = 0;
  _beepRemaining = 0;
  _beepStatus = LOW;
}

void Buzzer::beep(uint16_t ms) {
  beeps(ms, 1);
}

void Buzzer::beeps(uint16_t ms, uint8_t num) {
  _beepRemaining = num;
  _beepLength = ms;
  
  startBeep();
}

void Buzzer::startBeep() {
  _beepStatus = HIGH;
  _port.setStatus(_beepStatus);
  _stopTime = millis() + _beepLength;
}

void Buzzer::stopBeep() {
  _beepStatus = LOW;
  _port.setStatus(_beepStatus);
  _beepRemaining--;
  
  _stopTime = millis() + SHORT_BEEP;
}

void Buzzer::service() {
  if (millis() > _stopTime) {
    if (_beepRemaining > 0) {
      if (_beepStatus == HIGH) {
        stopBeep();
      } else {
        startBeep();
      }
    }
  }
}